﻿$(function () {
	//获取拿到的省、市、地区的id
	var prov_id = aprov_id;
	var city_id = acity_id;
	var area_id = aarea_id;
	$("#prov").html("");
	$("#city").html("");
	$("#area").html("");
	if (prov_id != null && prov_id != "" && city_id != null && city_id != "" && area_id != null && area_id != "") {
		//获取省，选中本来存的
		$.post(
	        "address/listProvince",
	        function(data) {
	            for(var prov in data){
	                $("#prov").append("<option value="+data[prov].province_id+">"+data[prov].province_name+"</option>");
	                if(prov_id == data[prov].province_id){
	                    $("#prov option[value='"+prov_id+"']").attr("selected",true);
	                }
	            }
	        },
	        "json"
		)
		//获取市，选中本来存的  
	    $.post(
	        "address/listCity",
	        {province_id:prov_id},
	        function(data) {
	            for(var city in data){
	                $("#city").append("<option value="+data[city].city_id+">"+data[city].city_name+"</option>");
	                if(city_id == data[city].city_id){
	                    $("#city option[value='"+city_id+"']").attr("selected",true);
	                }
	            }
	        },
	        "json"
	    )
	    //获取地区，选中本来存的
	    $.post(
	        "address/listArea",
	        {city_id:city_id},
	        function(data) {
	        	for(var area in data){
	                $("#area").append("<option value="+data[area].area_id+">"+data[area].area_name+"</option>");
	                if(area_id == data[area].area_id){
	                    $("#area option[value='"+area_id+"']").attr("selected",true);
	                }
	            }
	        },
	        "json"
	    )
	}else{
		//获取省，选中本来存的
		$.post(
	        "address/listProvince",
	        function(data) {
	            for(var prov in data){
	                $("#prov").append("<option value="+data[prov].province_id+">"+data[prov].province_name+"</option>");
	                if(prov_id == data[prov].province_id){
	                    $("#prov option[value='"+prov_id+"']").attr("selected",true);
	                }
	            }
	        },
	        "json"
		)
		$("#city").html("<option value=''>请选择城市</option>");
    	$("#area").html("<option value=''>请选择城市</option>");
	}
	
    //当省改变后，城市根据选择的省，发生相应的改变
    $("#prov").change(function(data){
    	$("#city").html("<option value=''>请选择城市</option>");
    	$("#area").html("<option value=''>请选择城市</option>");
    	var p = $("#prov").val();
    	$.post(
	         "address/listCity",
	         {province_id:p},
	         function(data) {
	             for(var city in data){
	                 $("#city").append("<option value="+data[city].city_id+">"+data[city].city_name+"</option>");
	             }
	         },
	         "json"
	     )
    })
    //当城市改变后，地区根据选择的城市，发生相应的改变
    $("#city").change(function(data){
    	$("#area").html("<option value=''>请选择城市</option>");
    	var c = $("#city").val();
    	$.post(
			 "address/listArea",
		     {city_id:c},
	         function(data) {
		    	 for(var area in data){
		             $("#area").append("<option value="+data[area].area_id+">"+data[area].area_name+"</option>");
		         }
	         },
	         "json"
	     )
    })
})     